﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderAggregator.DataStorage;
using OrderAggregator.InputFormats;

namespace OrderAggregator.UI
{
    public partial class ProductResolveControl : UserControl
    {
        private Storage.OrganizationRow _organization = null;

        public ProductResolveControl()
        {
            InitializeComponent();

            tableEditControl1.SelectedRowChanged += delegate
                                                        {
                                                            UpdateSelectedProduct();
                                                        };
        }

        /// <summary>
        /// Init
        /// </summary>
        /// <param name="organization"></param>
        /// <param name="order"></param>
        public void Init(Storage.OrganizationRow organization, BaseOrderRow order)
        {
            _organization = organization;

            lblCode.Text = order.ProductCode;
            txtBoxProduct.Text = order.ProductName;

            var product = ((Storage) organization.Table.DataSet).Product;
            tableEditControl1.Table = product;

            var productRow = product.FirstOrDefault(pr => pr.Name == order.ProductName);

            if (productRow == null)
            {
                try
                {
                    var pattern = order.ProductName.Split(' ').FirstOrDefault();
                    if (!string.IsNullOrEmpty(pattern))
                    {
                        pattern = pattern.Trim();

                        productRow = (Storage.ProductRow)
                            product.Select(string.Format("Name LIKE '*{0}*'", pattern))
                            .FirstOrDefault();
                    }
                }
                catch
                {
                    //nothin
                }
            }

            if (productRow != null)
            {
                tableEditControl1.SelectedRow = productRow;
            }



            UpdateOrganization();
            UpdateSelectedProduct();
        }

        public Storage.ProductRow Product
        {
            get
            {
                return tableEditControl1.SelectedRow as Storage.ProductRow;
            }
        }

        public Storage.OrganizationRow Organization
        {
            get { return _organization; }
        }

        private void UpdateSelectedProduct()
        {
            var product = this.Product;

            if (product == null)
            {
                txtBoxOurProduct.Text = "";
            }
            else
            {
                txtBoxOurProduct.Text = product.Name;
            }
        }

        private void UpdateOrganization()
        {
            if (_organization != null)
            {
                txtBoxOrganization.Text = _organization.Name;
            }
            else
            {
                txtBoxOrganization.Text = "";
            }
        }

        private void btnSelectOrg_Click(object sender, EventArgs e)
        {
            var organizationTree = new OrganizationTreeList();
            organizationTree.Init((Storage.OrganizationDataTable)_organization.Table);
            organizationTree.SelectedOrganization = _organization;

            using (var dialog = new EditDialog())
            {
                dialog.Text = "Организация:";
                dialog.HostedControl = organizationTree;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    if (organizationTree.SelectedOrganization != null)
                    {
                        _organization = organizationTree.SelectedOrganization;

                        UpdateOrganization();
                    }
                }
            }
        }
    }
}
